let date = new Date();
document.getElementsByClassName("name text-ellipsis")[0].innerHTML = "name";
document.getElementById("filmType").innerHTML = "type";
document.getElementById("fileTime").innerHTML = "time";
document.getElementById('filmLan').innerHTML = "language";
document.getElementsByClassName("value text-ellipsis screen")[0].innerHTML = "beginTime";
document.getElementsByClassName("phone-num")[0].innerHTML = "phoneNum";
document.getElementById("filmHall").innerHTML = getQueryVariable("hall").substring(0, 5) + "号厅";
// TODO: 向后端获取已经被选过的座位, 并且标记为不可选中状态

// document.getElementById("10_1").setAttribute("class", "seat sold");
// jQuery("#10_1").attr("data-st", "LK");

jQuery.ajax({
    type: 'post',
    url: "/order/get_seat",
    data: {
        "movieId": getQueryVariable("movieId"),
        "hall": decodeURIComponent(getQueryVariable("hall"))
    },
    success: function (body) {
        for (let i = 0; i < body.length; i++) {
            document.getElementById(body[i]).setAttribute("class", "seat sold");
            document.getElementById(body[i]).setAttribute("data-st", "LK");
        }
    }
});

// 获取电影信息
jQuery.ajax({
    type: 'post',
    url: '/seat_choose',
    data: {
        movieId: getQueryVariable("movieId")
    },
    success: function (body) {
        // 替换元素
        document.getElementById("cover").src = "./img/" + body[0].photo + "";
        document.getElementsByClassName("name text-ellipsis")[0].innerHTML = body[0].name;
        document.getElementById("filmType").innerHTML = body[0].types;
        document.getElementById("fileTime").innerHTML = body[0].duration;
        document.getElementById('filmLan').innerHTML = body[0].language;
        document.getElementsByClassName("value text-ellipsis screen")[0].innerHTML = "明天 " + (date.getMonth() + 1) + "月" + date.getDate() + " " + body[0].begintime;
        document.getElementsByClassName("phone-num")[0].innerHTML = body[0].phoneNum.substring(0, 3) + "*****" + body[0].phoneNum.substring(9);
    }
});

// TODO: 传座位参数给后端
function submit() {
    let id = getQueryVariable("movieId");
    let hall = decodeURIComponent(getQueryVariable("hall"));
    console.log(hall);
    console.log(document.getElementsByClassName("ticket")[0].getAttribute("data-index"));
    let seats = document.getElementsByClassName("ticket");

    let res = [];

    for (let i = 0; i < seats.length; i++) {
        res[i] = seats[i].getAttribute("data-index").replace("-", "_");
        console.log("res[]" + res[i]);
    }


    jQuery.ajax({
        type: 'post',
        url: '/order/seat_confirm',
        data: {
            "id": id,
            "hall": hall,
            "seats": res.toString()
        },
        success: function (body) {
            location.href = "../payment.html?orderNumber=" + body;
        }
    });
}

